    //
//  YXDownloadManagerViewController.m
//  MockProject
//
//  Created by FanYu on 22/8/2016.
//  Copyright © 2016 niuzhaowang. All rights reserved.
//

#import "YXDownloadManagerViewController.h"
#import "YXTopButtonView.h"
#import "YXBottomButtonView.h"
#import "Config.h"
#import "YXManagerScrollView.h"

@interface YXDownloadManagerViewController () <UIScrollViewDelegate, YXTopButtonViewDelegate, YXBottomButtonViewDelegate, YXManagerScrollViewDelegate, YXTaskListViewControllerDelegate>

@property (nonatomic, strong) YXTopButtonView *topButtonView;
@property (nonatomic, strong) YXBottomButtonView *bottomButtonView;
@property (nonatomic, strong) YXManagerScrollView *managerScrollView;

@end

@implementation YXDownloadManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    self.navigationItem.title = @"下载管理";
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    self.taskVC.delegate = self;
}


#pragma mark - Setup UI
- (void)setupUI {
    // self
    self.view.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];

    // scroll view
    self.managerScrollView = [[YXManagerScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.bounds.size.height)];
    self.managerScrollView.yxDelegate = self;
    [self.view addSubview:self.managerScrollView];
    
    // top button view
    self.topButtonView = [[YXTopButtonView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 44)];
    self.topButtonView.backgroundColor = [UIColor whiteColor];
    self.topButtonView.delegate = self;
    [self.view addSubview:self.topButtonView];

    //bottom button view
    self.bottomButtonView = [[YXBottomButtonView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 49)];
    self.bottomButtonView.delegate = self;    
}


#pragma mark - Helper
- (void)showBottomButtonView {
    self.bottomButtonView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 49);
    [self.view addSubview:self.bottomButtonView];
    
    [UIView animateWithDuration:0.2 animations:^{
        self.bottomButtonView.frame = CGRectMake(0, SCREEN_HEIGHT - 49, SCREEN_WIDTH, 49);
    } completion:^(BOOL finished) {
        
    }];
}
- (void)hideBottomButtonView {
    [UIView animateWithDuration:0.2 animations:^{
        self.bottomButtonView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 49);
    } completion:^(BOOL finished) {
        [self.bottomButtonView removeFromSuperview];
    }];
}


#pragma mark - Alert
- (void)showAlert:(NSString *)title {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
    [self.navigationController presentViewController:alertController animated:YES completion:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [alertController dismissViewControllerAnimated:YES completion:nil];
            [alertController removeFromParentViewController];
        });
    }];
}


#pragma mark - YXTaskListViewControllerDelegate
- (void)updateProgress:(NSProgress *)progress withTaskUID:(NSString *)uid {
    [self.managerScrollView updateProgress:progress withUID:uid];
}
- (void)updateSpeed:(NSString *)speed withTaskUID:(NSString *)uid {
    [self.managerScrollView updateSpeed:speed withUID:uid];
}
- (void)downloadFinishedForTaskUID:(NSString *)uid {
    [self.managerScrollView downloadFinishedForTask:uid];
}
- (void)downloadFailedForTaskUID:(NSString *)uid {
    [self.managerScrollView downloadFailedForTask:uid];
}
- (void)changeTaskStatusToDownloadingWithUID:(NSString *)uid {
    [self.managerScrollView changeTaskStatusToDownloadingWithUID:uid];
}
- (void)changeTaskStatusToWaitingWithUID:(NSString *)uid {
    [self.managerScrollView changeTaskStatusToWaitingWithUID:uid];
}
- (void)reachabilityChangedWithStatus:(NSString *)status {
    [self.managerScrollView reachabilityChangedWithStatus:status];
}

#pragma mark - YXTopButtonViewDelegate
- (void)topButtonTapped:(UIButton *)sender {
    [self.managerScrollView setContentOffset:CGPointMake(self.view.bounds.size.width * sender.tag, -64) animated:YES];
}


#pragma mark - YXBottomButtonViewDelegate
- (void)deleteButtonTapped:(UIButton *)sender {
    [self.managerScrollView commitDeleting];
}

- (void)selectAllButtonTapped:(UIButton *)sender {
    [self.managerScrollView commitSelectingAll];
}


#pragma mark - YXManagerScrollViewDelegate
- (void)sectionHeaderButtonTapped:(UIButton *)sender sectionHeaderType:(SectionHeaderType)type {
    switch (type) {
        case YXDownloadingSectionHeader:
            break;
            
        case YXDownloadedSectionHeader:
            if ([sender.titleLabel.text isEqualToString:@"取消"]) {
                [self hideBottomButtonView];
            } else {
                [self showBottomButtonView];
            }

            break;
            
        default:
            break;
    }
}

- (void)selectedCellAmount:(NSInteger)amount {
    [self.bottomButtonView updateSelectedAmount:amount];
}

- (void)scrollViewDidScrollTo:(NSInteger)page {
    [self.topButtonView setSelectedButton:page];
    
    //if (page == 0) {
    //    [self hideBottomButtonView];
    //    [self.managerScrollView.sectionHeaderView updateDownloadingButton];
    //}
}

- (void)hideBottomViewWhenScrolled {
    [self hideBottomButtonView];
}

- (void)showAlertWithTitle:(NSString *)title {
    [self showAlert:title];
}

@end
